﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SilverlightMediaFramework.Plugins.DualMedia;
using Nvidia.NVStereo3DPlugin;
using Microsoft.SilverlightMediaFramework.Plugins.Webcam;
using System.Collections.ObjectModel;
using Microsoft.SilverlightMediaFramework.Core.Media;
using Microsoft.SilverlightMediaFramework.Plugins.Primitives.S3D;
using Microsoft.SilverlightMediaFramework.Plugins.Primitives;

namespace LiveS3D
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            smf.MediaPluginRegistered += new EventHandler<Microsoft.SilverlightMediaFramework.Core.CustomEventArgs<Microsoft.SilverlightMediaFramework.Plugins.IMediaPlugin>>(smf_MediaPluginRegistered);
        }

        DualMediaPlugin S3DPlugin;
        void smf_MediaPluginRegistered(object sender, Microsoft.SilverlightMediaFramework.Core.CustomEventArgs<Microsoft.SilverlightMediaFramework.Plugins.IMediaPlugin> e)
        {
            if (NVidiaButton.IsChecked.Value)
            {
                Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current.Is3DPluginEnabled = false;
                NVStereo3DPlugin.Current.Is3DPluginEnabled = true;
            }
            else if (AnaglyphButton.IsChecked.Value)
            {
                NVStereo3DPlugin.Current.Is3DPluginEnabled = false;
                Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current.Is3DPluginEnabled = true;
            }
            else
            {
                NVStereo3DPlugin.Current.Is3DPluginEnabled = false;
                Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current.Is3DPluginEnabled = false;
            }

            S3DPlugin = e.Value as DualMediaPlugin;
            S3DPlugin.LeftSource = new WebcamMediaPlugin() { CaptureSource = LeftEye };
            S3DPlugin.RightSource = new WebcamMediaPlugin() { CaptureSource = RightEye };
        }

        CaptureSource LeftEye;
        CaptureSource RightEye;

        private void ChooseCameras()
        {
            if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                if (CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices().Count == 0)
                {
                    ErrorMessage.Visibility = System.Windows.Visibility.Visible;
                }
                else if (CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices().Count == 1)
                {
                    // set both to the same brush
                    WarningMessage.Visibility = System.Windows.Visibility.Visible;

                    CaptureSource source = new CaptureSource() { VideoCaptureDevice = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices().First() };
                    source.VideoCaptureDevice.DesiredFormat = WebcamMediaPlugin.GetBestFormat(source.VideoCaptureDevice.SupportedFormats);
                    LeftEye = source;
                    RightEye = source;
                    StartWebcam();
                }
                else
                {
                    WebcamChooser leftEyeChooser = new WebcamChooser();
                    leftEyeChooser.WebCams.ItemsSource = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();
                    leftEyeChooser.WebCams.SelectedIndex = 0;
                    leftEyeChooser.Show();
                    leftEyeChooser.Closed += new EventHandler(leftEyeChooser_Closed);
                }
            }
            else
            {
                ErrorMessage.Visibility = System.Windows.Visibility.Visible;
            }
        }

        void leftEyeChooser_Closed(object sender, EventArgs e)
        {
            WebcamChooser leftEyeChooser = sender as WebcamChooser;
            LeftEye = leftEyeChooser.Source;

            WebcamChooser rightEyeChooser = new WebcamChooser();
            rightEyeChooser.Title = "Choose the RIGHT eye camera";
            rightEyeChooser.WebCams.ItemsSource = leftEyeChooser.WebCams.ItemsSource.Cast<VideoCaptureDevice>().Where(d => leftEyeChooser.WebCams.SelectedItem != d);
            rightEyeChooser.WebCams.SelectedIndex = 0;
            rightEyeChooser.Show();
            rightEyeChooser.Closed += new EventHandler(rightEyeChooser_Closed);
        }

        void rightEyeChooser_Closed(object sender, EventArgs e)
        {
            WebcamChooser rightEyeChooser = sender as WebcamChooser;
            RightEye = rightEyeChooser.Source;
            StartWebcam();
        }

        //<Media:PlaylistItem MediaSource="webcam://fake" DeliveryMethod="NotSpecified">
        //    <Media:PlaylistItem.S3DProperties>
        //        <plugins3d:S3DProperties S3DEyePriority="LeftFirst" S3DFormat="SideBySide" S3DContent="Pair" S3DLeftEyePAR="1.0" S3DRightEyePAR="1.0" />
        //    </Media:PlaylistItem.S3DProperties>
        //</Media:PlaylistItem>
        void StartWebcam()
        {
            var Playlist = new ObservableCollection<PlaylistItem>(
                new[] 
                {
                    new PlaylistItem()
                    {
                        Title = "3D Webcam",
                        MediaSource = new Uri("webcam://fake"),
                        DeliveryMethod = DeliveryMethods.NotSpecified,
                        S3DProperties = new S3DProperties()
                        {
                            S3DEyePriority = S3D_EyePriorities.LeftFirst,
                            S3DFormat = S3D_Formats.SideBySide,
                            S3DContent = S3D_Contents.Pair,
                            S3DLeftEyePAR = 1.0,
                            S3DRightEyePAR = 1.0
                        }
                    }
                });
            smf.Playlist = Playlist;
        }

        private void ButtonSettings_Click(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "SettingsVisible", true);
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "SettingsHidden", true);
        }

        private void Anaglyph_Checked(object sender, RoutedEventArgs e)
        {
            if (NVStereo3DPlugin.Current != null)
            {
                NVStereo3DPlugin.Current.Is3DPluginEnabled = false;
            }
            if (Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current != null)
            {
                Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current.Is3DPluginEnabled = true;
            }

            if (S3DPlugin != null)
            {
                S3DPlugin.Unload();
                S3DPlugin.Load();
            }
        }

        private void NVidia_Checked(object sender, RoutedEventArgs e)
        {
            if (Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current != null)
            {
                Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current.Is3DPluginEnabled = false;
            }
            if (NVStereo3DPlugin.Current != null)
            {
                NVStereo3DPlugin.Current.Is3DPluginEnabled = true;
            }

            if (S3DPlugin != null)
            {
                S3DPlugin.Unload();
                S3DPlugin.Load();
            }
        }

        private void None_Checked(object sender, RoutedEventArgs e)
        {
            if (NVStereo3DPlugin.Current != null)
            {
                NVStereo3DPlugin.Current.Is3DPluginEnabled = false;
            }
            if (Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current != null)
            {
                Microsoft.SilverlightMediaFramework.Plugins.Anaglyph3D.Anaglyph3DPlugin.Current.Is3DPluginEnabled = false;
            }

            if (S3DPlugin != null)
            {
                S3DPlugin.Unload();
                S3DPlugin.Load();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StupidStartButton.Visibility = System.Windows.Visibility.Collapsed;
            ChooseCameras();
        }
    }
}
